using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace tomware.OpenIddict.UI.Identity.Api;

[Route("roles")]
public class RoleController : IdentityApiController
{
  private readonly IRoleApiService _service;

  public RoleController(IRoleApiService service)
  {
    _service = service;
  }

  [HttpGet]
  [ProducesResponseType(typeof(IEnumerable<RoleViewModel>), StatusCodes.Status200OK)]
  public async Task<IActionResult> GetRolesAsync()
  {
    var result = await _service.GetRolesAsync();

    return Ok(result);
  }

  [HttpGet("{id}")]
  [ProducesResponseType(typeof(RoleViewModel), StatusCodes.Status200OK)]
  public async Task<IActionResult> GetAsync(string id)
  {
    if (id == null)
    {
      return BadRequest();
    }

    var result = await _service.GetAsync(id);
    if (result == null)
    {
      return NotFound();
    }

    return Ok(result);
  }

  [HttpPost]
  [ProducesResponseType(StatusCodes.Status201Created)]
  public async Task<IActionResult> CreateAsync([FromBody] RoleViewModel model)
  {
    if (model == null)
    {
      return BadRequest();
    }

    if (!ModelState.IsValid)
    {
      return BadRequest(ModelState);
    }

    var result = await _service.CreateAsync(model);

    return Created($"roles/{result}", result);
  }

  [HttpPut]
  [ProducesResponseType(StatusCodes.Status204NoContent)]
  public async Task<IActionResult> UpdateAsync([FromBody] RoleViewModel model)
  {
    if (model == null)
    {
      return BadRequest();
    }

    if (!ModelState.IsValid)
    {
      return BadRequest(ModelState);
    }

    await _service.UpdateAsync(model);

    return NoContent();
  }

  [HttpDelete("{id}")]
  [ProducesResponseType(StatusCodes.Status204NoContent)]
  public async Task<IActionResult> DeleteAsync(string id)
  {
    if (id == null)
    {
      return BadRequest();
    }

    await _service.DeleteAsync(id);

    return NoContent();
  }
}
